package com.woniuxy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.SickBed;
import com.woniuxy.entity.Tde;
import com.woniuxy.entity.WardManage;
import com.woniuxy.entity.dto.WardDTO;
import com.woniuxy.mapper.SickBedMapper;
import com.woniuxy.mapper.TdeMapper;
import com.woniuxy.mapper.WardManageMapper;

import com.woniuxy.producer.DelayExchangeProducer;
import com.woniuxy.service.SaasService;
import com.woniuxy.service.WardManagerService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class WardManagerServiceImpl implements WardManagerService , SaasService {

    @Resource
    private WardManageMapper wardManageMapper;
    @Resource
    private TdeMapper tdeMapper;
    @Resource
    private SickBedMapper sickBedMapper;

    @Autowired
    private DelayExchangeProducer delayExchangeProducer;

    //根据条件分页查询病房
    @Override
    public ResponseData<?> queryWard(PageParam<WardManage> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<WardManage> wardManagementList = wardManageMapper.queryList(pageParam.getParam());
        return new ResponseData<>().ok(new PageInfo<>(wardManagementList));
    }

    //添加病床
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseData<?> addWard(WardManage wardManage) {

        //判断前端传输的数据是否为空 数字类型要先判断是否为空
        if (StringUtils.isBlank(wardManage.getSrNo()) || wardManage.getSrPrice() == null || StrUtil.isBlank(wardManage.getSrPrice().toString()) ||
                wardManage.getSrTotalNum() == null || StrUtil.isBlank(String.valueOf(wardManage.getSrTotalNum())) ||
                wardManage.getSrDepartmentId() == null ||StrUtil.isBlank(String.valueOf(wardManage.getSrDepartmentId()))) {

            return new ResponseData<>().fail(ResponseEnum.MANDATORY_EMPTY);
        }

        String wardStr = JSON.toJSONString(wardManage);

        //异步插入病房和病床
        delayExchangeProducer.send(wardStr,"addWard",0);

        return new ResponseData<>().ok();
    }

    //查询所有部门
    @Override
    public ResponseData<?> queryTde() {
        List<Tde> tdeList = tdeMapper.queryList();
        return new ResponseData<>().ok(tdeList);
    }

    //根据id查询病房

    @Override
    public ResponseData<?> queryWardById(Integer id) {
        WardManage wardManage = wardManageMapper.queryWardById(id);
        return new ResponseData<>().ok(wardManage);
    }

    //修改病房
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseData<?> updateWard(WardManage wardManage) {
//        判断病房有无病人居住
        List<SickBed> sickBedList = sickBedMapper.querySickBedByRoomId(wardManage.getId());
        for (SickBed sickBed:sickBedList){
            if (sickBed.getBStatus().equals("1")){
                return new ResponseData<>().fail(ResponseEnum.MANDATORY_EMPTY);
            }
        }
        //修改病房要先把之前的病床状态全部改为不可用 再根据修改后的病房值进行添加病床
        wardManageMapper.updateWard(wardManage);

        List<Integer> ids = new ArrayList<>();
        for (SickBed sickBed : sickBedList){
            ids.add(sickBed.getId());
        }
        sickBedMapper.upDateSbStatus(ids);

        List<SickBed> bedArrayList = new ArrayList<>();
        for (int i = 1; i <= wardManage.getSrTotalNum(); i++) {
            SickBed sickBed = new SickBed();
            sickBed.setBNo(wardManage.getId()+"A"+i+"号床");
            sickBed.setBRoomId(wardManage.getId());
            bedArrayList.add(sickBed);
        }
        sickBedMapper.addSickBed(bedArrayList);

        return new ResponseData<>().ok();
    }

    //删除病房
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseData<?> deleteWard(Integer id) {
//        判断当前病房有无病人
        List<SickBed> sickBedList = sickBedMapper.querySickBedByRoomId(id);
        for (SickBed sickBed:sickBedList){
            if (sickBed.getBStatus().equals("1")){
                return new ResponseData<>().fail(ResponseEnum.MANDATORY_EMPTY);
            }
        }
        WardManage wardManage = new WardManage();
        wardManage.setId(id);
        wardManage.setSrStatus("1");
        //删除病房同时把该病房的病床也修改为不存在
        //修改病房状态为不存在
        wardManageMapper.updateWard(wardManage);

        List<Integer> ids = new ArrayList<>();
        for (SickBed sickBed : sickBedList){
            ids.add(sickBed.getId());
        }
        sickBedMapper.upDateSbStatus(ids);

        return new ResponseData<>().ok();
    }
}
